﻿using CustomCharacter;
using Exiled.API.Extensions;
using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;
using InventorySystem.Items.Firearms.Attachments;
using InventorySystem.Items.Firearms;
using InventorySystem;
using MEC;
using CustomCharacter.VoiceChat;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Exiled.API.Enums;
using CustomCharacter.Ability;
using CustomCharacter.Interfaces;
using RgCore.API;
using RgCore.CustomSide;
using RgCore.PlayerHud;

namespace Scp1867
{
    [RoleInfo("SCP-1867", "Human", PlayerRoles.RoleTypeId.ClassD, PlayerRoles.RoleTypeId.Tutorial)]
    public class Scp1867 : CustomCharacter.CustomCharacter, IRoundStartSpawn, IScpNumber, ITrialCard, ICustomSpawnpoint
    {
        public Scp1867(int id) : base(id)
        {
        }
        public Vector3 SpawnPosition => RoleExtensions.GetRandomSpawnLocation(PlayerRoles.RoleTypeId.FacilityGuard).Position;
        public override Color32 RoleColor => new Color32(82, 173, 156, 255);

        public byte MinPlayers => 30;

        public int Cost => 150;

        public string ScpNumber => "1867";
        public override void Role_Deactive()
        {
            base.Role_Deactive();
            Timing.CallDelayed(1.0f, delegate ()
            {
                Player.VC_RemoveListener(VoiceChat.VoiceChatChannel.ScpChat);
                Player.Scale = Vector3.one;
            });
        }
        public static AbilityOutput Ability1(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            switch (trigger)
            {
                case AbilityTrigger.Active:
                    if (info.CooldownStatus.Status == CooldownStatus<Player>.CdStatus.Activating || info.CooldownStatus.Status == CooldownStatus<Player>.CdStatus.LongActivating)
                    {
                        return new AbilityOutput(true, true, 0, "已主动停止侦听");
                    }
                    player.VC_AddListener(VoiceChat.VoiceChatChannel.ScpChat);
                    return new AbilityOutput(true, true, 30, "正在偷听SCP频道");
                case AbilityTrigger.Deactive:
                    info.CooldownStatus.StartCD();
                    player.VC_RemoveListener(VoiceChat.VoiceChatChannel.ScpChat);
                    return new AbilityOutput(true);
                default:
                    return new AbilityOutput(false);
            }
        }
        public static AbilityOutput Ability2(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            string text = "当前有以下SCP于您的区域中：";
            foreach(var target in Player.List.Where(x => x.GetSide() == Side.Scp && x.Zone == player.Zone))
            {
                string name = $"<color=red>{target.Role.Base.RoleName}</color>";
                float distance = Vector3.Distance(target.Position, player.Position);
                if (target.IsCustomCharacter())
                    name = target.GetCustomRoleName();
                text += $"\n{name} - <color=yellow>{(int)distance}m</color>";
            }
            player.SendRawMsg("scp1867", text, 10, ScreenLocation.CenterBottom, true);
            return new AbilityOutput(true);
        }
        public static AbilityOutput Ability3(Player player, ref AbilityInfo info, IExiledEvent e, AbilityTrigger trigger)
        {
            switch (trigger)
            {
                case AbilityTrigger.Active:
                    if (info.CooldownStatus.Status == CooldownStatus<Player>.CdStatus.Activating || info.CooldownStatus.Status == CooldownStatus<Player>.CdStatus.LongActivating)
                    {
                        return new AbilityOutput(true, true, 0, "");
                    }
                    player.Scale = new Vector3(1f, 0.2f, 1f);
                    return new AbilityOutput(true, true, 10, "海公牛！");
                case AbilityTrigger.Deactive:
                    info.CooldownStatus.StartCD();
                    player.Scale = Vector3.one;
                    return new AbilityOutput(true);
                default:
                    return new AbilityOutput(false);
            }
        }

        public override IEnumerable<Ability> AbilityRegister()
        {
            return new Ability[] {
                new Ability("Scp1867_1", "监听SCP频道30秒", AbilityType.Offensive, 60, Ability1, null, null, null, GetType()),
                new Ability("Scp1867_2", "探测当前区域SCP玩家与距离", AbilityType.Offensive, 30, Ability2, null, null, null, GetType()),
                new Ability("Scp1867_3", "矮化自己10秒", AbilityType.Offensive, 30, Ability3, null, null, null, GetType())
            };
        }

        public override void Role_Activate()
        {
            base.Role_Activate();
            Timing.CallDelayed(1.0f, delegate ()
            {
                Player.Inventory.ServerAddAmmo(ItemType.Ammo9x19, 100);
                Player.Inventory.ServerAddItem(ItemType.KeycardZoneManager);
                Firearm firearm = (Firearm)Player.Inventory.ServerAddItem(ItemType.GunCrossvec);
                if (AttachmentsServerHandler.PlayerPreferences.TryGetValue(Player.ReferenceHub, out Dictionary<ItemType, uint> dictionary) && dictionary.TryGetValue(firearm.ItemTypeId, out uint code))
                {
                    firearm.ApplyAttachmentsCode(code, true);
                }
                FirearmStatusFlags firearmStatusFlags = FirearmStatusFlags.MagazineInserted;
                if (firearm.HasAdvantageFlag(AttachmentDescriptiveAdvantages.Flashlight))
                {
                    firearmStatusFlags |= FirearmStatusFlags.FlashlightEnabled;
                }
                firearm.Status = new FirearmStatus(firearm.AmmoManagerModule.MaxAmmo, firearmStatusFlags, firearm.GetCurrentAttachmentsCode());
            });
        }
    }
}
